/*
 * @Descripttion: 
 * @Author: ziwei
 * @Date: 2022-07-06 16:01:28
 * @LastEditors: ziwei
 * @LastEditTime: 2022-07-06 17:27:16
 */
export{}
//1" 从赋的值中推断出来 类型从右向左流动
let foo = 1
let bar ='xx'

//2.底部流出
//通过return关键字推断返回值的类型
function add(a:number,b:number){
    //return a+b
}
let c =add(1,2)

//3.从左向右流程 Sum推出a和b
type Sum = (a:number,b:number)=>number
let sum:Sum = (a,b)=>{
    return a+b
}
let person ={
    name:'xx',
    age:11
}
// let name = person.name
// let age = person.age
let {name,age} = person
let numbers = [1,2,3]
let c2 = numbers[0]

//interface也可以做推断
interface DefaultProps{
    name?:string;
    age?:number
}
let defaultProps:DefaultProps={
    name:'XX',
    age:10
}
let props={
    ...defaultProps,
    home:'北京'
}
type Props = typeof props
function addOne(a:any){
    return a+1;
}
function sum3(a:number,b:number){
    return a+addOne(b)
}
let k = sum3(1,2) //number+any = 得到any 


interface Bird{
    name:string
    fly():void
}
interface Person{
    talk():void
}
type BirdPerson = Bird&Person
let p1:BirdPerson ={
    name:'xx',
    fly() {},
    talk() {},
}
p1.fly()
p1.talk()

//mixin
interface AnyObject{
    [prop:string]:any
}
function mixin<T,U>(one:T,two:U){
    const result = <(T&U)>{}
    for(let key in one){
        (<T>result)[key] = one[key]
    }
    for(let key in two){
        (<U>result)[key] = two[key]
    }
    return result
}

const x  = mixin({name:'xx'},{age:11})
console.log(x.name,x.age)

//typeof 
//先定义类型 在定义变量
type Person3 = {
    name:string
}
let p3:Person3 = {
    name:'xx'
}
let p4={
    name:'xx'
}
type P4 = typeof p4

//索引访问操作符
interface Person5{
    name:string
    age:number
    job:{
        name:string
    }
}
let FrontEndJob:Person5['job'] ={
    name:'前端'
}

//映射类型
interface Person6{
    name:string
    age:number
    gender:'male'|'female'
}
//批量把一个接口中的属性全部变成可选的
type PartialPerson = {
    [key in keyof Person6]?:Person6[key]
}
type Partial<T>  ={
    [key in keyof T]?:T[key]
}
type PPerson = Partial<Person6>

